package controllers

import (
	"HeartTimesCrm/models"
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"strings"
)

//用户信息相关API
type UserInfoController struct {
	beego.Controller
}

func (this *UserInfoController) URLMapping() {
	this.Mapping("GetBaseInfo", this.GetBaseInfo)
	this.Mapping("GetExtInfo", this.GetExtInfo)
	this.Mapping("Signin", this.Signin)
	this.Mapping("UpdateInfo", this.UpdateInfo)
	this.Mapping("GetSculpture", this.GetSculpture)
	this.Mapping("UpdateSculpture", this.UpdateSculpture)

}

func UserAuth(cxt *context.Context) {
	beego.BeeLogger.Debug("UserAuth...")
	value := cxt.Input.CruSession.Get("utag")
	if _, ok := value.(*UserTag); !ok {
		res := &Response{1, "users need to visit"}
		cxt.Output.Json(res, false, false)
	}
}

func (this *UserInfoController) getUserId() int {
	uTag := this.GetSession("utag").(*UserTag)
	return uTag.Uid
}

// @router /base [get]
func (this *UserInfoController) GetBaseInfo() {
	var fields []string
	values := this.GetString("field")
	if values != "" {
		fields = strings.Split(values, ",")
	}
	beego.BeeLogger.Debug("UserInfo::GetBaseInfo fields: %v", fields)

	res := new(Response)
	user, err := models.GetBaseInfo(this.getUserId(), fields...)
	if err != nil {
		res.ErrNo = -1
		res.Data = err.Error()
	} else {
		res.Data = user.GetFilterData(fields)
	}
	this.Data["json"] = res
	this.ServeJson()
}

// @router /ext [get]
func (this *UserInfoController) GetExtInfo() {
	var fields []string
	values := this.GetString("field")
	if values != "" {
		fields = strings.Split(values, ",")
	}
	beego.BeeLogger.Debug("UserInfo::GetExtInfo fields: %v", fields)

	res := new(Response)
	userExt, err := models.GetExtInfo(this.getUserId(), fields...)
	if err != nil {
		res.ErrNo = -1
		res.Data = err.Error()
	} else {
		res.Data = userExt.GetFilterData(fields)
	}
	this.Data["json"] = res
	this.ServeJson()
}

// @router /signin [get]
func (this *UserInfoController) Signin() {
	userId := this.getUserId()
	beego.BeeLogger.Debug("UserInfo::Signin, userid:%d", userId)
	info, err := models.Signin(userId)
	res := new(Response)
	if err != nil {
		res.ErrNo = -1
		res.Data = err.Error()
	} else {
		res.Data = info.GetFilterData([]string{"signin_date", "signin_days",
			"signin_unit", "integral"})
	}
	this.Data["json"] = res
	this.ServeJson()
}

// @router /update [put]
func (this *UserInfoController) UpdateInfo() {
	data := this.Ctx.Input.RequestBody
	beego.BeeLogger.Debug("UserInfo::UpdateInfo data: %s", string(data))

	var baseData map[string]interface{}
	err := json.Unmarshal(data, &baseData)
	if err == nil {
		err = models.UpdateInfo(this.getUserId(), baseData)
	}

	res := new(Response)
	if err != nil {
		res.ErrNo = -1
		res.Data = err.Error()
	}
	this.Data["json"] = res
	this.ServeJson()
}

// @router /sculpture [get]
func (this *UserInfoController) GetSculpture() {
	imageid, err := this.GetInt("imageid", -1)
	beego.BeeLogger.Debug("UserInfo::GetSculpture imageid:%s", imageid)
	if err != nil || imageid == -1 {
		this.Data["json"] = &Response{-1, "imageid not set or wrong in url!"}
		this.ServeJson()
	} else {
		image, err := models.GetSculpture(imageid)
		if err != nil {
			this.Data["json"] = &Response{-1, err.Error()}
			this.ServeJson()
		} else {
			this.Ctx.Output.ContentType(image.Format)
			this.Ctx.Output.Body([]byte(image.Content))
		}
	}
}

// @router /sculpture [put]
func (this *UserInfoController) UpdateSculpture() {
	format := this.GetString("format")
	beego.BeeLogger.Debug("UserInfo::UpdateSculpture format:%s", format)

	image := new(models.Image)
	image.Format = format
	image.Content = string(this.Ctx.Input.RequestBody)
	image, err := models.AddSculpture(image)
	res := new(Response)
	if err != nil {
		res.ErrNo = -1
		res.Data = err.Error()
	} else {
		//更新用户imageid信息
		data := map[string]interface{}{"image_id": image.Id}
		models.UpdateInfo(this.getUserId(), data)
		//更新session中保存的imageid
		uTag := this.GetSession("utag").(*UserTag)
		models.DelSculpture(uTag.ImageId)
		uTag.ImageId = image.Id
		this.SetSession("utag", uTag)
	}
	this.Data["json"] = res
	this.ServeJson()
}
